home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kjs / value.h < prev   
Encoding:
C/C++ Source or Header  |  2005-10-10  |  12.0 KB  |  401 lines

  1. // -*- c-basic-offset: 2 -*-
  2. /*
  3.  *  This file is part of the KDE libraries
  4.  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
  5.  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
  6.  *  Copyright (C) 2003 Apple Computer, Inc.
  7.  *
  8.  *  This library is free software; you can redistribute it and/or
  9.  *  modify it under the terms of the GNU Library General Public
  10.  *  License as published by the Free Software Foundation; either
  11.  *  version 2 of the License, or (at your option) any later version.
  12.  *
  13.  *  This library is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *  Library General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU Library General Public License
  19.  *  along with this library; see the file COPYING.LIB.  If not, write to
  20.  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21.  *  Boston, MA 02110-1301, USA.
  22.  *
  23.  */
  24.  
  25. #ifndef _KJS_VALUE_H_
  26. #define _KJS_VALUE_H_
  27.  
  28. #include <stdlib.h> // Needed for size_t
  29.  
  30. #include "ustring.h"
  31. #include "simple_number.h"
  32.  
  33. // Primitive data types
  34.  
  35. namespace KJS {
  36.  
  37.   class Value;
  38.   class ValueImp;
  39.   class ValueImpPrivate;
  40.   class Undefined;
  41.   class UndefinedImp;
  42.   class Null;
  43.   class NullImp;
  44.   class Boolean;
  45.   class BooleanImp;
  46.   class String;
  47.   class StringImp;
  48.   class Number;
  49.   class NumberImp;
  50.   class Object;
  51.   class ObjectImp;
  52.   class Reference;
  53.   class List;
  54.   class ListImp;
  55.   class Completion;
  56.   class ExecState;
  57.  
  58.   /**
  59.    * Primitive types
  60.    */
  61.   enum Type {
  62.     UnspecifiedType = 0,
  63.     UndefinedType   = 1,
  64.     NullType        = 2,
  65.     BooleanType     = 3,
  66.     StringType      = 4,
  67.     NumberType      = 5,
  68.     ObjectType      = 6
  69.   };
  70.  
  71.   /**
  72.    * ValueImp is the base type for all primitives (Undefined, Null, Boolean,
  73.    * String, Number) and objects in ECMAScript.
  74.    *
  75.    * Note: you should never inherit from ValueImp as it is for primitive types
  76.    * only (all of which are provided internally by KJS). Instead, inherit from
  77.    * ObjectImp.
  78.    */
  79.   class KJS_EXPORT ValueImp {
  80.     friend class Collector;
  81.     friend class Value;
  82.     friend class ContextImp;
  83.   public:
  84.     ValueImp();
  85.     virtual ~ValueImp();
  86.  
  87.     ValueImp* ref() { if (!SimpleNumber::is(this)) refcount++; return this; }
  88.     bool deref() { if (SimpleNumber::is(this)) return false; else return (!--refcount); }
  89.  
  90.     virtual void mark();
  91.     bool marked() const;
  92.     void* operator new(size_t);
  93.     void operator delete(void*);
  94.  
  95.     /**
  96.      * @internal
  97.      *
  98.      * set by Object() so that the collector is allowed to delete us
  99.      */
  100.     void setGcAllowed();
  101.  
  102.     // Will crash if called on a simple number.
  103.     void setGcAllowedFast() { _flags |= VI_GCALLOWED; }
  104.  
  105.     int toInteger(ExecState *exec) const;
  106.     int toInt32(ExecState *exec) const;
  107.     unsigned int toUInt32(ExecState *exec) const;
  108.     unsigned short toUInt16(ExecState *exec) const;
  109.  
  110.     // Dispatch wrappers that handle the special small number case
  111.  
  112.     Type dispatchType() const;
  113.     Value dispatchToPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const;
  114.     bool dispatchToBoolean(ExecState *exec) const;
  115.     double dispatchToNumber(ExecState *exec) const;
  116.     UString dispatchToString(ExecState *exec) const;
  117.     bool dispatchToUInt32(unsigned&) const;
  118.     Object dispatchToObject(ExecState *exec) const;
  119.  
  120.     unsigned short int refcount;
  121.  
  122.     bool isDestroyed() const { return _flags & VI_DESTRUCTED; }
  123.  
  124.   private:
  125.     unsigned short int _flags;
  126.  
  127.     virtual Type type() const = 0;
  128.  
  129.     // The conversion operations
  130.  
  131.     virtual Value toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const = 0;
  132.     virtual bool toBoolean(ExecState *exec) const = 0;
  133.     virtual double toNumber(ExecState *exec) const = 0;
  134.     // TODO: no need for the following 4 int conversions to be virtual
  135.     virtual UString toString(ExecState *exec) const = 0;
  136.     virtual Object toObject(ExecState *exec) const = 0;
  137.     virtual bool toUInt32(unsigned&) const;
  138.  
  139.     enum {
  140.       VI_MARKED = 1,
  141.       VI_GCALLOWED = 2,
  142.       VI_CREATED = 4,
  143.       VI_DESTRUCTED = 8   // nice word we have here :)
  144.     }; // VI means VALUEIMPL
  145.  
  146.     ValueImpPrivate *_vd;
  147.  
  148.     // Give a compile time error if we try to copy one of these.
  149.     ValueImp(const ValueImp&);
  150.     ValueImp& operator=(const ValueImp&);
  151.   };
  152.  
  153.   /**
  154.    * Value objects are act as wrappers ("smart pointers") around ValueImp
  155.    * objects and their descendents. Instead of using ValueImps
  156.    * (and derivatives) during normal program execution, you should use a
  157.    * Value-derived class.
  158.    *
  159.    * Value maintains a pointer to a ValueImp object and uses a reference
  160.    * counting scheme to ensure that the ValueImp object is not deleted or
  161.    * garbage collected.
  162.    *
  163.    * Note: The conversion operations all return values of various types -
  164.    * if an error occurs during conversion, an error object will instead
  165.    * be returned (where possible), and the execution state's exception
  166.    * will be set appropriately.
  167.    */
  168.   class KJS_EXPORT Value {
  169.   public:
  170.     Value() : rep(0) { }
  171.     explicit Value(ValueImp *v);
  172.     Value(const Value &v);
  173.     ~Value();
  174.  
  175.     Value& operator=(const Value &v);
  176.     /**
  177.      * Returns whether or not this is a valid value. An invalid value
  178.      * has a 0 implementation pointer and should not be used for
  179.      * any other operation than this check. Current use: as a
  180.      * distinct return value signalling failing dynamicCast() calls.
  181.      */
  182.     bool isValid() const { return rep != 0; }
  183.     /**
  184.      * @deprecated
  185.      * Use !isValid() instead.
  186.      */
  187.     bool isNull() const { return rep == 0; }
  188.     ValueImp *imp() const { return rep; }
  189.  
  190.     /**
  191.      * Returns the type of value. This is one of UndefinedType, NullType,
  192.      * BooleanType, StringType, NumberType, or ObjectType.
  193.      *
  194.      * @return The type of value
  195.      */
  196.     Type type() const { return rep->dispatchType(); }
  197.  
  198.     /**
  199.      * Checks whether or not the value is of a particular tpye
  200.      *
  201.      * @param t The type to compare with
  202.      * @return true if the value is of the specified type, otherwise false
  203.      */
  204.     bool isA(Type t) const { return rep->dispatchType() == t; }
  205.  
  206.     /**
  207.      * Performs the ToPrimitive type conversion operation on this value
  208.      * (ECMA 9.1)
  209.      */
  210.     Value toPrimitive(ExecState *exec,
  211.                       Type preferredType = UnspecifiedType) const
  212.       { return rep->dispatchToPrimitive(exec, preferredType); }
  213.  
  214.     /**
  215.      * Performs the ToBoolean type conversion operation on this value (ECMA 9.2)
  216.      */
  217.     bool toBoolean(ExecState *exec) const { return rep->dispatchToBoolean(exec); }
  218.  
  219.     /**
  220.      * Performs the ToNumber type conversion operation on this value (ECMA 9.3)
  221.      */
  222.     double toNumber(ExecState *exec) const { return rep->dispatchToNumber(exec); }
  223.  
  224.     /**
  225.      * Performs the ToInteger type conversion operation on this value (ECMA 9.4)
  226.      */
  227.     int toInteger(ExecState *exec) const { return rep->toInteger(exec); }
  228.  
  229.     /**
  230.      * Performs the ToInt32 type conversion operation on this value (ECMA 9.5)
  231.      */
  232.     int toInt32(ExecState *exec) const { return rep->toInt32(exec); }
  233.  
  234.     /**
  235.      * Performs the ToUInt32 type conversion operation on this value (ECMA 9.6)
  236.      */
  237.     unsigned int toUInt32(ExecState *exec) const { return rep->toUInt32(exec); }
  238.  
  239.     /**
  240.      * Performs the ToUInt16 type conversion operation on this value (ECMA 9.7)
  241.      */
  242.     unsigned short toUInt16(ExecState *exec) const { return rep->toUInt16(exec); }
  243.  
  244.     /**
  245.      * Performs the ToString type conversion operation on this value (ECMA 9.8)
  246.      */
  247.     UString toString(ExecState *exec) const { return rep->dispatchToString(exec); }
  248.  
  249.     /**
  250.      * Performs the ToObject type conversion operation on this value (ECMA 9.9)
  251.      */
  252.     Object toObject(ExecState *exec) const;
  253.  
  254.     /**
  255.      * Checks if we can do a lossless conversion to UInt32.
  256.      */
  257.     bool toUInt32(unsigned& i) const { return rep->dispatchToUInt32(i); }
  258.  
  259.   protected:
  260.     ValueImp *rep;
  261.   };
  262.  
  263.   // Primitive types
  264.  
  265.   /**
  266.    * Represents an primitive Undefined value. All instances of this class
  267.    * share the same implementation object, so == will always return true
  268.    * for any comparison between two Undefined objects.
  269.    */
  270.   class KJS_EXPORT Undefined : public Value {
  271.   public:
  272.     Undefined();
  273.  
  274.     /**
  275.      * Converts a Value into an Undefined. If the value's type is not
  276.      * UndefinedType, a null object will be returned (i.e. one with it's
  277.      * internal pointer set to 0). If you do not know for sure whether the
  278.      * value is of type UndefinedType, you should check the isValid()
  279.      * methods afterwards before calling any methods on the returned value.
  280.      *
  281.      * @return The value converted to an Undefined
  282.      */
  283.     static Undefined dynamicCast(const Value &v);
  284.   private:
  285.     friend class UndefinedImp;
  286.     explicit Undefined(UndefinedImp *v);
  287.  
  288.   };
  289.  
  290.   /**
  291.    * Represents an primitive Null value. All instances of this class
  292.    * share the same implementation object, so == will always return true
  293.    * for any comparison between two Null objects.
  294.    */
  295.   class KJS_EXPORT Null : public Value {
  296.   public:
  297.     Null();
  298.  
  299.     /**
  300.      * Converts a Value into an Null. If the value's type is not NullType,
  301.      * a null object will be returned (i.e. one with it's internal pointer set
  302.      * to 0). If you do not know for sure whether the value is of type
  303.      * NullType, you should check the isValid() methods afterwards before
  304.      * calling any methods on the returned value.
  305.      *
  306.      * @return The value converted to a Null
  307.      */
  308.     static Null dynamicCast(const Value &v);
  309.   private:
  310.     friend class NullImp;
  311.     explicit Null(NullImp *v);
  312.   };
  313.  
  314.   /**
  315.    * Represents an primitive Boolean value
  316.    */
  317.   class KJS_EXPORT Boolean : public Value {
  318.   public:
  319.     Boolean(bool b = false);
  320.  
  321.     /**
  322.      * Converts a Value into an Boolean. If the value's type is not BooleanType,
  323.      * a null object will be returned (i.e. one with it's internal pointer set
  324.      * to 0). If you do not know for sure whether the value is of type
  325.      * BooleanType, you should check the isValid() methods afterwards before
  326.      * calling any methods on the returned value.
  327.      *
  328.      * @return The value converted to a Boolean
  329.      */
  330.     static Boolean dynamicCast(const Value &v);
  331.  
  332.     bool value() const;
  333.   private:
  334.     friend class BooleanImp;
  335.     explicit Boolean(BooleanImp *v);
  336.   };
  337.  
  338.   /**
  339.    * Represents an primitive String value
  340.    */
  341.   class KJS_EXPORT String : public Value {
  342.   public:
  343.     String(const UString &s = "");
  344.  
  345.     /**
  346.      * Converts a Value into an String. If the value's type is not StringType,
  347.      * a null object will be returned (i.e. one with it's internal pointer set
  348.      * to 0). If you do not know for sure whether the value is of type
  349.      * StringType, you should check the isValid() methods afterwards before
  350.      * calling any methods on the returned value.
  351.      *
  352.      * @return The value converted to a String
  353.      */
  354.     static String dynamicCast(const Value &v);
  355.  
  356.     UString value() const;
  357.   private:
  358.     friend class StringImp;
  359.     explicit String(StringImp *v);
  360.   };
  361.  
  362.   extern const double NaN;
  363.   extern const double Inf;
  364.  
  365.   /**
  366.    * Represents an primitive Number value
  367.    */
  368.   class KJS_EXPORT Number : public Value {
  369.     friend class ValueImp;
  370.   public:
  371.     Number(int i);
  372.     Number(unsigned int u);
  373.     Number(double d = 0.0);
  374.     Number(long int l);
  375.     Number(long unsigned int l);
  376.  
  377.     double value() const;
  378.     int intValue() const;
  379.  
  380.     bool isNaN() const;
  381.     bool isInf() const;
  382.  
  383.     /**
  384.      * Converts a Value into an Number. If the value's type is not NumberType,
  385.      * a null object will be returned (i.e. one with it's internal pointer set
  386.      * to 0). If you do not know for sure whether the value is of type
  387.      * NumberType, you should check the isNull() methods afterwards before
  388.      * calling any methods on the returned value.
  389.      *
  390.      * @return The value converted to a Number
  391.      */
  392.     static Number dynamicCast(const Value &v);
  393.   private:
  394.     friend class NumberImp;
  395.     explicit Number(NumberImp *v);
  396.   };
  397.  
  398. } // namespace
  399.  
  400. #endif // _KJS_VALUE_H_
  401.